<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="报表名称" prop="rptName">
        <el-input
          v-model="queryParams.rptName"
          placeholder="请输入报表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item prop="rptDateStart">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.rptDateStart"
          type="month"
          value-format="yyyy-MM"
          placeholder="开始日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="至 " label-width="30px" prop="rptDateEnd">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.rptDateEnd"
          type="month"
          value-format="yyyy-MM"
          placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['attendance_color:attendance_color:export']"
        >导出</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5"> -->
      <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['attendance_color:attendance_color:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['attendance_color:attendance_color:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['attendance_color:attendance_color:remove']"
          >删除</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['attendance_color:attendance_color:import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-s-check"
          size="mini"
          @click="handleCheck"
          v-hasPermi="['report:rptrecord:check']"
          >审核</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="attendance_colorList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <!-- <el-table-column label="报表名称" align="center" prop="rptName" /> -->
      <el-table-column
        label="考勤日期"
        align="center"
        prop="rptDate"
        width="180"
      >
        <template slot-scope="scope">
          <!-- <span>{{ parseTime(scope.row.rptDate, '{y}-{m}-{d}') }}</span> -->
          <span>{{ parseTime(scope.row.rptDate, "{y}-{m}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor" />
      <!-- <el-table-column label="表头HTML" align="center" prop="rptHear" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['attendance_color:attendance_color:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['attendance_color:attendance_color:remove']"
            >删除</el-button
          >

          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport(scope.row)"
            v-hasPermi="['attendance_color:attendance_color:export']"
            >导出</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤统计对话框 -->
    <el-dialog v-myFullscreen :title="title" :visible.sync="open" append-to-body fullscreen>
      <div v-html="form.rptHear" style="width: 3000px"></div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog v-myFullscreen
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
      fullscreen
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="
          upload.url +
          '?updateSupport=' +
          upload.updateSupport +
          '&updateFlag=' +
          upload.updateFlag
        "
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateFlag" /><el-link
            type="info"
            style="font-size: 12px"
            >是否更新已经存在的用户数据</el-link
          >
          <br /><br />
          <el-date-picker
            :editable="false"
            clearable
            size="small"
            v-model="upload.updateSupport"
            type="month"
            value-format="yyyy-MM"
            placeholder="选择日期"
          >
          </el-date-picker>
          <br /><br />
          <el-link type="info" style="font-size: 12px" @click="importTemplate"
            >下载模板</el-link
          >
        </div>

        <div class="el-upload__tip" style="color: white" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  updateattendance_colorSH,
  listAttendance_color,
  getAttendance_color,
  delAttendance_color,
  addAttendance_color,
  updateAttendance_color,
} from "@/api/basic/attendance/attendance_color";
import { getToken } from "@/utils/auth";
import { getInfo } from "@/api/login";
import { listUser } from "@/api/system/user";
export default {
  name: "Attendance_color",
  data() {
    return {
      nickName: "",
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProductAttendanceColorSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤统计表格数据
      attendance_colorList: [],
      // 考勤统计子表格数据
      productAttendanceColorSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: "",
        updateFlag: true,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/basic/attendance_color/importData",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rptName: null,
        rptDate: null,
        rptHear: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.queryUserList();
  },
  methods: {
    /** 查询考勤统计列表 */
    getList() {
      for (let i = 0; i < 50; i++) {
        var temp =
          "sub.setA" + (i + 1) + "(String.valueOf(list.get(" + i + ")));";
        // console.log(temp);
      }

      this.loading = true;
      listAttendance_color(this.queryParams).then((response) => {
        this.attendance_colorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 审核按钮操作 */
    handleCheck(row) {
      /* 审核 */
      this.ids.forEach((id) => {
        // console.log(this.nickName);
        let obj = {};
        obj.id = id;
        obj.auditor = this.nickName;
        /* 修改 */
        updateattendance_colorSH(obj).then((response) => {
          this.msgSuccess("审核成功");
          this.getList();
        });
      });
    },
    /* 查询用户列表 */
    queryUserList() {
      listUser().then((response) => {
        this.userList = response.rows;
      });
      getInfo().then((response) => {
        this.nickName = response.user.nickName;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        rptName: null,
        rptDate: null,
        rptMonth: null,
        rptYear: null,
        rptHear: null,
        fileurl: null,
        code: null,
        factory: null,
        rptType: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.productAttendanceColorSubList = [];
      this.resetForm("form");
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加考勤统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAttendance_color(id).then((response) => {
        this.form = response.data;

        this.productAttendanceColorSubList =
          response.data.productAttendanceColorSubList;
        this.open = true;
        this.title = "修改考勤统计";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAttendance_color(id).then((response) => {
        this.form = response.data;
                // console.log(this.form)
        this.productAttendanceColorSubList =
          response.data.productAttendanceColorSubList;
        this.open = true;
        this.title = "查看考勤统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.productAttendanceColorSubList =
            this.productAttendanceColorSubList;
          if (this.form.id != null) {
            updateAttendance_color(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendance_color(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除考勤统计编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAttendance_color(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 考勤统计子序号 */
    rowProductAttendanceColorSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 考勤统计子添加按钮操作 */
    handleAddProductAttendanceColorSub() {
      let obj = {};
      obj.ao = "";
      obj.a1 = "";
      obj.a2 = "";
      obj.a3 = "";
      obj.a4 = "";
      obj.a5 = "";
      obj.a6 = "";
      obj.a7 = "";
      obj.a8 = "";
      obj.a9 = "";
      obj.remark = "";
      obj.a10 = "";
      obj.a11 = "";
      obj.a12 = "";
      obj.a13 = "";
      obj.a14 = "";
      obj.a15 = "";
      obj.a16 = "";
      obj.a17 = "";
      obj.a18 = "";
      obj.a19 = "";
      obj.a20 = "";
      obj.a21 = "";
      obj.a22 = "";
      obj.a23 = "";
      obj.a24 = "";
      obj.a25 = "";
      obj.a26 = "";
      obj.a27 = "";
      obj.a28 = "";
      obj.a29 = "";
      obj.a30 = "";
      obj.a31 = "";
      obj.a32 = "";
      obj.a33 = "";
      obj.a34 = "";
      obj.a35 = "";
      obj.a36 = "";
      obj.a37 = "";
      obj.a38 = "";
      obj.a39 = "";
      obj.a40 = "";
      obj.a41 = "";
      obj.a42 = "";
      obj.a43 = "";
      obj.a44 = "";
      obj.a45 = "";
      obj.a46 = "";
      obj.a47 = "";
      obj.a48 = "";
      obj.a49 = "";
      obj.a50 = "";
      this.productAttendanceColorSubList.push(obj);
    },
    /** 考勤统计子删除按钮操作 */
    handleDeleteProductAttendanceColorSub() {
      if (this.checkedProductAttendanceColorSub.length == 0) {
        this.$alert("请先选择要删除的考勤统计子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.productAttendanceColorSubList.splice(
          this.checkedProductAttendanceColorSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleProductAttendanceColorSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.productAttendanceColorSub.clearSelection();
        this.$refs.productAttendanceColorSub.toggleRowSelection(
          selection.pop()
        );
      } else {
        this.checkedProductAttendanceColorSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport(row) {
     
     window.location.href=row.fileurl;
     // this.download(
      //   "basic/attendance_color/export",
      //   {
      //     ...row,
      //   },
      //   "考勤统计" + this.parseTime(row.rptDate, "{y}-{m}") + ".xlsx"
      // );
    },
    /** 下载模板操作 */
    importTemplate() {
      window.location.href="http://172.16.1.67:9300/statics/TemplateColor.xlsx";
      // this.download(
      //   "basic/attendance_color/exportTemplate",
      //   {
      //     ...this.queryParams,
      //   },
      //   `考勤统计模板.xlsx`
      // );
    },
  },
};
</script>